<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: AdminSettingsController.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_AdminSettingsController extends Core_Controller_Action_Admin {
	
	//ACTION FOR GLOBAL SETTINGS
	public function indexAction() {
		
		if ($this->getRequest ()->isPost ()) {
			if ($_POST ['sitepage_lsettings']) {
				$_POST ['sitepage_lsettings'] = trim ( $_POST ['sitepage_lsettings'] );
			}
		}
		$sitepage_global_form_content = array ('sitepage_location', 'sitepage_report', 'sitepage_share', 'sitepage_socialshare', 'sitepage_printer', 'sitepage_tellafriend', 'sitepage_captcha_post', 'sitepage_proximitysearch', 'sitepage_checkcomment_widgets', 'sitepage_sponsored_image', 'sitepage_sponsored_color', 'sitepage_feature_image', 'sitepage_featured_color', 'sitepage_page', 'sitepage_proximity_search_kilometer', 'sitepage_addfavourite_show', 'sitepage_title_truncation', 'sitepage_claimlink', 'sitepage_claim_show_menu', 'sitepage_contact', 'sitepage_requried_description', 'sitepage_status_show', 'sitepage_manageadmin', 'sitepage_layoutcreate', 'sitepage_communityads', 'sitepage_locationfield', 'sitepage_price_field', 'sitepage_category_edit', 'sitepage_package_enable', 'submit', "sitepage_payment_benefit", "sitepage_currency", 'sitepage_network', "sitepage_networks_type", "sitepage_browseorder", 'sitepage_requried_photo', 'sitepage_showmore', 'sitepage_show_menu', "sitepage_default_show", 'sitepage_map_sponsored', "sitepage_photolightbox_show", 'sitepage_photolightbox_bgcolor', 'sitepage_photolightbox_fontcolor', "sitepage_map_city", "sitepage_addfavourite_show", "sitepage_map_zoom", "sitepage_feed_type", "sitepage_manifestUrlP", "sitepage_manifestUrlS", "sitepage_mylike_show", "sitepage_categorywithslug" );
		$oldLocation = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.map.city', "World" );
		include_once APPLICATION_PATH . '/application/modules/Sitepage/controllers/license/license1.php';
		$newLocation = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.map.city', "World" );
		$this->setDefaultMapCenterPoint ( $oldLocation, $newLocation );
		$this->view->isModsSupport = Engine_Api::_ ()->getApi ( 'suggestion', 'sitepage' )->isModulesSupport ();
	}
	
	//ACTION FOR FAQ
	public function faqAction() {
		
		//TABS CREATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_faq' );
		
		$this->view->faq = 1;
		$this->view->faq_type = $this->_getParam ( 'faq_type', 'general' );
	}
	
	//ACTION FOR GETTING THE CATGEORIES AND SUBCATEGORIES
	public function sitepagecategoriesAction() {
		
		//GET NAVIGATION
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_sitepagecategories' );
		
		//GET TASK
		if (isset ( $_POST ['task'] )) {
			$task = $_POST ['task'];
		} elseif (isset ( $_GET ['task'] )) {
			$task = $_GET ['task'];
		} else {
			$task = "main";
		}
		
		//GET CATEGORIES TABLE
		$tableCategories = Engine_Api::_ ()->getDbTable ( 'categories', 'sitepage' );
		
		//GET CATEGORIES TABLE NAME
		$tableCategoriesName = $tableCategories->info ( 'name' );
		
		//GET PAGE TABLE
		$tablePage = Engine_Api::_ ()->getDbtable ( 'pages', 'sitepage' );
		
		if ($task == "savecat") {
			//GET CATEGORY ID
			$cat_id = $_GET ['cat_id'];
			
			$cat_title_withoutparse = $_GET ['cat_title'];
			
			//GET CATEGORY TITLE
			$cat_title = Engine_Api::_ ()->sitepage ()->parseString ( $_GET ['cat_title'] );
			
			//GET CATEGORY DEPENDANCY
			$cat_dependency = $_GET ['cat_dependency'];
			$subcat_dependency = $_GET ['subcat_dependency'];
			if ($cat_title == "") {
				if ($cat_id != "new") {
					if ($cat_dependency == 0) {
						$row_ids = Engine_Api::_ ()->getDbtable ( 'categories', 'sitepage' )->getSubCategories ( $cat_id );
						foreach ( $row_ids as $values ) {
							$tableCategories->delete ( array ('subcat_dependency = ?' => $values->category_id, 'cat_dependency = ?' => $values->category_id ) );
							$tableCategories->delete ( array ('category_id = ?' => $values->category_id ) );
						}
						
						$tablePage->update ( array ('category_id' => 0, 'subcategory_id' => 0 ), array ('category_id = ?' => $cat_id ) );
						$tableCategories->delete ( array ('category_id = ?' => $cat_id ) );
						
						//START SITEPAGEREVIEW CODE
						if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
							Engine_Api::_ ()->sitepagereview ()->deleteCategory ( $cat_id );
						}
					
		//END SITEPAGEREVIEW CODE
					} else {
						$tableCategories->update ( array ('category_name' => $cat_title ), array ('category_id = ?' => $cat_id, 'cat_dependency = ?' => $cat_dependency ) );
						$tablePage->update ( array ('category_id' => 0, 'subcategory_id' => 0 ), array ('category_id = ?' => $cat_id ) );
						$tableCategories->delete ( array ('cat_dependency = ?' => $cat_id, 'subcat_dependency = ?' => $cat_id ) );
						$tableCategories->delete ( array ('category_id = ?' => $cat_id ) );
					}
				}
				//SEND AJAX CONFIRMATION
				echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'><script type='text/javascript'>";
				echo "window.parent.removecat('$cat_id');";
				echo "</script></head><body></body></html>";
				exit ();
			} else {
				if ($cat_id == 'new') {
					$row_info = $tableCategories->fetchRow ( $tableCategories->select ()->from ( $tableCategoriesName, 'max(cat_order) AS cat_order' ) );
					$cat_order = $row_info ['cat_order'] + 1;
					$row = $tableCategories->createRow ();
					$row->category_name = $cat_title_withoutparse;
					$row->cat_order = $cat_order;
					$row->cat_dependency = $cat_dependency;
					$row->subcat_dependency = $subcat_dependency;
					$newcat_id = $row->save ();
				} else {
					$tableCategories->update ( array ('category_name' => $cat_title_withoutparse ), array ('category_id = ?' => $cat_id ) );
					$newcat_id = $cat_id;
				}
				
				//SEND AJAX CONFIRMATION
				echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'><script type='text/javascript'>";
				echo "window.parent.savecat_result('$cat_id', '$newcat_id', '$cat_title', '$cat_dependency', '$subcat_dependency');";
				echo "</script></head><body></body></html>";
				exit ();
			}
		} elseif ($task == "changeorder") {
			$divId = $_GET ['divId'];
			$sitepageOrder = explode ( ",", $_GET ['sitepageorder'] );
			//RESORT CATEGORIES
			if ($divId == "categories") {
				for($i = 0; $i < count ( $sitepageOrder ); $i ++) {
					$cat_id = substr ( $sitepageOrder [$i], 4 );
					$tableCategories->update ( array ('cat_order' => $i + 1 ), array ('category_id = ?' => $cat_id ) );
				}
			} elseif (substr ( $divId, 0, 7 ) == "subcats") {
				for($i = 0; $i < count ( $sitepageOrder ); $i ++) {
					$cat_id = substr ( $sitepageOrder [$i], 4 );
					$tableCategories->update ( array ('cat_order' => $i + 1 ), array ('category_id = ?' => $cat_id ) );
				}
			} elseif (substr ( $divId, 0, 11 ) == "treesubcats") {
				for($i = 0; $i < count ( $sitepageOrder ); $i ++) {
					$cat_id = substr ( $sitepageOrder [$i], 4 );
					$tableCategories->update ( array ('cat_order' => $i + 1 ), array ('category_id = ?' => $cat_id ) );
				}
			}
		}
		
		$categories = array ();
		$category_info = Engine_Api::_ ()->getDbTable ( 'categories', 'sitepage' )->getCategories ( 1 );
		foreach ( $category_info as $value ) {
			$sub_cat_array = array ();
			$subcategories = Engine_Api::_ ()->getDbtable ( 'categories', 'sitepage' )->getAllCategories ( $value->category_id, 'subcategory_id', 0, 'subcategory_id', 0 );
			foreach ( $subcategories as $subresults ) {
				$subsubcategories = Engine_Api::_ ()->getDbtable ( 'categories', 'sitepage' )->getAllCategories ( $subresults->category_id, 'subsubcategory_id', 0, 'subsubcategory_id', 0 );
				$treesubarrays [$subresults->category_id] = array ();
				foreach ( $subsubcategories as $subsubcategoriesvalues ) {
					$treesubarray = array ('tree_sub_cat_id' => $subsubcategoriesvalues->category_id, 'tree_sub_cat_name' => $subsubcategoriesvalues->category_name, 'order' => $subsubcategoriesvalues->cat_order, 'count' => $subsubcategoriesvalues->count );
					$treesubarrays [$subresults->category_id] [] = $treesubarray;
				}
				
				$tmp_array = array ('sub_cat_id' => $subresults->category_id, 'sub_cat_name' => $subresults->category_name, 'tree_sub_cat' => $treesubarrays [$subresults->category_id], 'count' => $subresults->count, 'order' => $subresults->cat_order );
				$sub_cat_array [] = $tmp_array;
			}
			
			$category_array = array ('category_id' => $value->category_id, 'category_name' => $value->category_name, 'order' => $value->cat_order, 'count' => $value->count, 'sub_categories' => $sub_cat_array );
			$categories [] = $category_array;
		}
		
		include APPLICATION_PATH . '/application/modules/Sitepage/controllers/license/license2.php';
	}
	
	public function readmeAction() {
		
		$this->view->faq = 0;
		$this->view->faq_type = $this->_getParam ( 'faq_type', 'general' );
	}
	
	//ACTION FOR SHOWING THE PAGE STATISTICS
	public function statisticAction() {
		
		//GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_statistic' );
		
		//GET PAGE TABLE
		$tablePage = Engine_Api::_ ()->getDbtable ( 'pages', 'sitepage' );
		
		//GET TOTAL PAGES    
		$this->view->totalSitepage = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totalpage' ) )->totalpage;
		
		//GET PUBLISH PAGES
		$this->view->totalPublish = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totalpublish' )->where ( 'draft =?', 1 ) )->totalpublish;
		
		//GET DRAFTED PAGES
		$this->view->totalDrafted = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totaldrafted' )->where ( 'draft =?', 0 ) )->totaldrafted;
		
		//Get CLOSED PAGES
		$this->view->totalClosed = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totalclosed' )->where ( 'closed =?', 1 ) )->totalclosed;
		
		//Get OPEN PAGES
		$this->view->totalopen = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totalopen' )->where ( 'closed =?', 0 ) )->totalopen;
		
		//GET APPROVED PAGES
		$this->view->totalapproved = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totalapproved' )->where ( 'approved =?', 1 ) )->totalapproved;
		
		//GET DISAPPROVED PAGES
		$this->view->totaldisapproved = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totaldisapproved' )->where ( 'approved =?', 0 ) )->totaldisapproved;
		
		//GET FEATURED PAGES
		$this->view->totalfeatured = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totalfeatured' )->where ( 'featured =?', 1 ) )->totalfeatured;
		
		//GET SPONSORED PAGES	
		$this->view->totalsponsored = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'count(*) AS totalsponsored' )->where ( 'sponsored =?', 1 ) )->totalsponsored;
		
		//GET TOTAL COMMENTS IN PAGES	
		$this->view->totalcommentpost = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'sum(comment_count) AS totalcomments' ) )->totalcomments;
		
		//GET TOTAL LIKES IN PAGES	
		$this->view->totallikepost = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'sum(like_count) AS totallikes' ) )->totallikes;
		
		//GET TOTAL VIEWS IN PAGES	
		$this->view->totalviewpost = $tablePage->fetchRow ( $tablePage->select ()->from ( $tablePage->info ( 'name' ), 'sum(view_count) AS totalviews' ) )->totalviews;
		
		//CHECK THAT SITEPAGE REVIEW IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
			//GET REVIEW TABLE TABLE
			$tableReview = Engine_Api::_ ()->getDbtable ( 'reviews', 'sitepagereview' );
			
			//GET TOTAL REVIEWS IN PAGES	
			$this->view->totalreview = $tableReview->fetchRow ( Engine_Api::_ ()->getDbtable ( 'reviews', 'sitepagereview' )->select ()->from ( $tableReview->info ( 'name' ), 'count(*) AS totalreview' ) )->totalreview;
		}
		
		//CHECK THAT SITEPAGE DISCUSSION IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagediscussion' )) {
			//GET DISCUSSION TABLE
			$tableDiscussion = Engine_Api::_ ()->getDbtable ( 'topics', 'sitepage' );
			
			//GET TOTAL DISCUSSION IN PAGES
			$this->view->totaldiscussion = $tableDiscussion->fetchRow ( $tableDiscussion->select ()->from ( $tableDiscussion->info ( 'name' ), 'count(*) AS totaldiscussion' ) )->totaldiscussion;
			
			//GET DISCUSSION POST TABLE
			$tableDiscussionPost = Engine_Api::_ ()->getDbtable ( 'posts', 'sitepage' );
			
			//GET TOTAL DISCUSSION POST (REPLY)IN PAGES       
			$this->view->totaldiscussionpost = $tableDiscussionPost->fetchRow ( $tableDiscussionPost->select ()->from ( $tableDiscussionPost->info ( 'name' ), 'count(*) AS totalpost' ) )->totalpost;
		}
		
		//GET PHOTO TABLE
		$tablePhoto = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' );
		
		//GET THE TOTAL PHOTO IN PAGES
		$this->view->totalphotopost = $tablePhoto->fetchRow ( $tablePhoto->select ()->from ( $tablePhoto->info ( 'name' ), 'count(*) AS totalphoto' )->where ( 'collection_id <>?', 0 ) )->totalphoto;
		
		//CHECK THAT SITEPAGE ALBUM IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
			//GET ALBUM TABLE
			$tableAlbum = Engine_Api::_ ()->getDbtable ( 'albums', 'sitepage' );
			
			//GET THE TOTAL ALBUM IN PAGES
			$this->view->totalalbumpost = $tableAlbum->fetchRow ( $tableAlbum->select ()->from ( $tableAlbum->info ( 'name' ), 'count(*) AS totalalbum' ) )->totalalbum;
		}
		
		//CHECK THAT SITEPAGE NOTE IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagenote' )) {
			//GET NOTE TABLE
			$tableNote = Engine_Api::_ ()->getDbtable ( 'notes', 'sitepagenote' );
			
			//GET THE TOTAL NOTE IN PAGES
			$this->view->totalnotepost = $tableNote->fetchRow ( $tableNote->select ()->from ( $tableNote->info ( 'name' ), 'count(*) AS totalnotes' ) )->totalnotes;
		}
		
		//CHECK THAT SITEPAGE VIDEO IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagevideo' )) {
			//GET VIDEO TABLE
			$tableVideo = Engine_Api::_ ()->getDbtable ( 'videos', 'sitepagevideo' );
			
			//GET THE TOTAL VIDEO IN PAGES
			$this->view->totalvideopost = $tableVideo->fetchRow ( $tableVideo->select ()->from ( $tableVideo->info ( 'name' ), 'count(*) AS totalvideos' ) )->totalvideos;
		}
		
		//CHECK THAT SITEPAGE DOCUMENT IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagedocument' )) {
			//GET DOCUMENT TABLE
			$tableDocument = Engine_Api::_ ()->getDbtable ( 'documents', 'sitepagedocument' );
			
			//GET THE TOTAL DOCUMENT IN PAGES
			$this->view->totaldocumentpost = $tableDocument->fetchRow ( $tableDocument->select ()->from ( $tableDocument->info ( 'name' ), 'count(*) AS totaldocuments' ) )->totaldocuments;
		}
		
		//CHECK THAT SITEPAGE EVENT IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageevent' )) {
			//GET EVENT TABLE
			$tableEvent = Engine_Api::_ ()->getDbtable ( 'events', 'sitepageevent' );
			
			//GET THE TOTAL EVENT IN PAGES
			$this->view->totaleventpost = $tableEvent->fetchRow ( $tableEvent->select ()->from ( $tableEvent->info ( 'name' ), 'count(*) AS totalevents' ) )->totalevents;
		}
		
		//CHECK THAT SITEPAGE VIDEO IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' )) {
			//GET PLAYLIST TABLE
			$tablePlaylist = Engine_Api::_ ()->getDbtable ( 'playlists', 'sitepagemusic' );
			
			//GET THE TOTAL PLAYLIST IN PAGES
			$this->view->totalplaylists = $tablePlaylist->fetchRow ( $tablePlaylist->select ()->from ( $tablePlaylist->info ( 'name' ), 'count(*) AS totalplaylists' ) )->totalplaylists;
			
			//GET PLAYLIST TABLE
			$tableSongs = Engine_Api::_ ()->getDbtable ( 'playlistSongs', 'sitepagemusic' );
			
			//GET THE TOTAL PLAYLIST IN PAGES
			$this->view->totalsongs = $tableSongs->fetchRow ( $tableSongs->select ()->from ( $tableSongs->info ( 'name' ), 'count(*) AS totalsongs' ) )->totalsongs;
		}
		
		//CHECK THAT SITEPAGE POLL IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagepoll' )) {
			//GET POLL TABLE
			$tablePoll = Engine_Api::_ ()->getDbtable ( 'polls', 'sitepagepoll' );
			
			//GET THE TOTAL POLL IN PAGES
			$this->view->totalpollpost = $tablePoll->fetchRow ( $tablePoll->select ()->from ( $tablePoll->info ( 'name' ), 'count(*) AS totalpolls' ) )->totalpolls;
		}
		
		//CHECK THAT SITEPAGE OFFER IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageoffer' )) {
			//GET OFFER TABLE
			$tableOffer = Engine_Api::_ ()->getDbtable ( 'offers', 'sitepageoffer' );
			
			//GET THE TOTAL OFFER IN PAGES
			$this->view->totalofferpost = $tableOffer->fetchRow ( $tableOffer->select ()->from ( $tableOffer->info ( 'name' ), 'count(*) AS totaloffers' ) )->totaloffers;
		}
	}
	
	public function graphAction() {
		
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_graph' );
		$this->view->form = $form = new Sitepage_Form_Admin_Settings_Graph ();
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
			include APPLICATION_PATH . '/application/modules/Sitepage/controllers/license/license2.php';
		}
	}
	
	//ACTION FOR EMAIL THE DETAIL
	public function emailAction() {
		
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_email' );
		$this->view->form = $form = new Sitepage_Form_Admin_Settings_Email ();
		
		//check if comments should be displayed or not
		$show_comments = Engine_Api::_ ()->sitepage ()->displayCommentInsights ();
		
		$taskstable = Engine_Api::_ ()->getDbtable ( 'tasks', 'core' );
		$rtasksName = $taskstable->info ( 'name' );
		$taskstable_result = $taskstable->select ()->from ( $rtasksName, array ('processes', 'timeout' ) )->where ( 'title = ?', 'Sitepage Insight Mail' )->where ( 'plugin = ?', 'Sitepage_Plugin_Task_InsightNotification' )->limit ( 1 );
		$prefields = $taskstable->fetchRow ( $taskstable_result );
		
		//populate form
		$form->populate ( array ('sitepage_insightemail' => $prefields->processes ) );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
			include APPLICATION_PATH . '/application/modules/Sitepage/controllers/license/license2.php';
			if ($values ['sitepage_demo'] == 1 && $values ['sitepage_insightemail'] == 1) {
				
				$view = Zend_Registry::isRegistered ( 'Zend_View' ) ? Zend_Registry::get ( 'Zend_View' ) : null;
				
				$site_title = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.site.title', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'core.general.site.title', 1 ) );
				$site_title_color = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.title.color', "#ffffff" );
				$site_header_color = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.header.color', "#79b4d4" );
				$insights_string = '';
				$template_header = "";
				$template_footer = "";
				
				$insights_string .= "<table cellpadding='2'><tr><td><table cellpadding='2'><tr><td><span style='font-size: 14px; font-weight: bold;'>" . $view->translate ( "Sample Page" ) . "</span></td></tr>";
				
				$template_header .= "<table width='98%' cellspacing='0' border='0'><tr><td width='100%' bgcolor='#f7f7f7' style='font-family:arial,tahoma,verdana,sans-serif;padding:40px;'><table width='620' cellspacing='0' cellpadding='0' border='0'>";
				$template_header .= "<tr><td style='background:" . $site_header_color . "; color:$site_title_color;font-weight:bold;font-family:arial,tahoma,verdana,sans-serif; padding: 4px 8px;vertical-align:middle;font-size:16px;text-align: left;' nowrap='nowrap'>" . $site_title . "</td></tr><tr><td valign='top' style='background-color:#fff; border-bottom: 1px solid #ccc; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; font-family:arial,tahoma,verdana,sans-serif; padding: 15px;padding-top:0;' colspan='2'><table width='100%'><tr><td colspan='2'>";
				
				if ($values ['sitepage_insightmail_options'] == 1) {
					$vals ['days_string'] = 'week';
				} elseif ($values ['sitepage_insightmail_options'] == 2) {
					$vals ['days_string'] = 'month';
				}
				$path = 'http://' . $_SERVER ['HTTP_HOST'] . $view->baseUrl ();
				$insight_link = "<a style='color: rgb(59, 89, 152); text-decoration: none;' href='" . $path . "'>" . $view->translate ( 'Visit your Insights Page' ) . "</a>";
				$update_link = "<a style='color: rgb(59, 89, 152); text-decoration: none;' href='" . $path . "'>" . $view->translate ( 'Send an update to people who like this' ) . "</a>";
				
				//check if Communityad Plugin is enabled
				$sitepagecommunityadEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'communityad' );
				$adversion = null;
				if ($sitepagecommunityadEnabled) {
					$communityadmodulemodule = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->getModule ( 'communityad' );
					$adversion = $communityadmodulemodule->version;
					if ($adversion >= '4.1.5') {
						$promote_Ad_link = "<a style='color: rgb(59, 89, 152); text-decoration: none;' href='" . $path . "'>" . $view->translate ( 'Promote with %s Ads', $site_title ) . "</a>";
					}
				}
				
				$insights_string .= "<tr><td><span style='font-size: 24px; font-family: arial;'>" . '2' . "</span>\t<span style='color: rgb(85, 85, 85);'>" . $vals ['days_string'] . $view->translate ( 'ly active users' ) . "</span></td></tr><tr><td><span style='font-size: 24px; font-family: arial;'>" . '2' . "</span>\t<span style='color: rgb(85, 85, 85);'>" . $view->translate ( 'people like this' ) . "</span>&nbsp;<span style='font-size: 18px; font-family: arial;' >" . '2' . "</span>\t<span style='color: rgb(85, 85, 85);' >" . $view->translate ( 'since last' ) . "\t" . $vals ['days_string'] . "</span></td></tr>";
				if (! empty ( $show_comments )) {
					$insights_string .= "<tr><td><span style='font-size: 24px; font-family: arial;'>" . '2' . "</span>\t<span style='color: rgb(85, 85, 85);'>" . $view->translate ( 'comments' ) . "</span>&nbsp;<span style='font-size: 18px; font-family: arial;' >" . '2' . "</span>\t<span style='color: rgb(85, 85, 85);' >" . $view->translate ( 'since last' ) . "\t" . $vals ['days_string'] . "</span></td></tr>";
				}
				$insights_string .= "<tr><td><span style='font-size: 24px; font-family: arial;'>" . '10' . "</span>\t <span style='color: rgb(85, 85, 85);'>" . $view->translate ( 'visits' ) . "</span>&nbsp;<span style='font-size: 18px; font-family: arial;' >" . '5' . "</span>\t<span style='color: rgb(85, 85, 85);' >" . $view->translate ( 'since last' ) . "\t" . $vals ['days_string'] . "</span></td></tr></table></td></tr>
				<tr><td>" . "<ul style=' padding-left: 5px;'><li>" . $insight_link . "</li><li>" . $update_link;
				
				//check if Communityad Plugin is enabled
				if ($sitepagecommunityadEnabled && $adversion >= '4.1.5') {
					$insights_string .= "</li><li>" . $promote_Ad_link;
				}
				$insights_string .= "</li></ul></tr></td></table>";
				
				$template_footer .= "</td></tr></table></td></tr></td></table></td></tr></table>";
				$days_string = ucfirst ( $vals ['days_string'] );
				$owner_name = Engine_Api::_ ()->user ()->getViewer ()->getTitle ();
				$email = Engine_Api::_ ()->getApi ( 'settings', 'core' )->core_mail_from;
				Engine_Api::_ ()->getApi ( 'mail', 'core' )->sendSystem ( $values ['sitepage_admin'], 'SITEPAGE_INSIGHTS_EMAIL_NOTIFICATION', array ('recipient_title' => $owner_name, 'template_header' => $template_header, 'message' => $insights_string, 'template_footer' => $template_footer, 'site_title' => $site_title, 'days' => $days_string, 'email' => $email, 'queue' => true ) );
			}
		}
	}
	
	//ACTION FOR AD SHOULD BE DISPLAY OR NOT ON PAGES
	public function adsettingsAction() {
		
		//GET NAVIGATION
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_adsettings' );
		
		//FORM
		$this->view->form = $form = new Sitepage_Form_Admin_Adsettings ();
		
		//CHECK THAT COMMUNITY AD PLUGIN IS ENABLED OR NOT
		$communityadEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'communityad' );
		if ($communityadEnabled) {
			$this->view->ismoduleenabled = $ismoduleenabled = 1;
		} else {
			$this->view->ismoduleenabled = $ismoduleenabled = 0;
		}
		
		//CHECK THAT SITEPAGE DOCUMENT PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagedocument' ) && $ismoduleenabled) {
			$this->view->isdocumentenabled = 1;
		} else {
			$this->view->isdocumentenabled = 0;
		}
		
		//CHECK THAT SITEPAGE NOTE PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagenote' ) && $ismoduleenabled) {
			$this->view->isnoteenabled = 1;
		} else {
			$this->view->isnoteenabled = 0;
		}
		
		//CHECK THAT SITEPAGE ALBUM PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' ) && $ismoduleenabled) {
			$this->view->isalbumenabled = 1;
		} else {
			$this->view->isalbumenabled = 0;
		}
		
		//CHECK THAT SITEPAGE VIDEO PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagevideo' ) && $ismoduleenabled) {
			$this->view->isvideoenabled = 1;
		} else {
			$this->view->isvideoenabled = 0;
		}
		
		//CHECK THAT SITEPAGE EVENT PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageevent' ) && $ismoduleenabled) {
			$this->view->iseventenabled = 1;
		} else {
			$this->view->iseventenabled = 0;
		}
		
		//CHECK THAT SITEPAGE DISCUSSION PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagediscussion' ) && $ismoduleenabled) {
			$this->view->isdiscussionenabled = 1;
		} else {
			$this->view->isdiscussionenabled = 0;
		}
		
		//CHECK THAT SITEPAGE POLL PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagepoll' ) && $ismoduleenabled) {
			$this->view->ispollenabled = 1;
		} else {
			$this->view->ispollenabled = 0;
		}
		
		//CHECK THAT SITEPAGE REVIEW PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' ) && $ismoduleenabled) {
			$this->view->isreviewenabled = 1;
		} else {
			$this->view->isreviewenabled = 0;
		}
		
		//CHECK THAT SITEPAGE OFFER PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageoffer' ) && $ismoduleenabled) {
			$this->view->isofferenabled = 1;
		} else {
			$this->view->isofferenabled = 0;
		}
		
		//CHECK THAT SITEPAGE FORM PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageform' ) && $ismoduleenabled) {
			$this->view->isformenabled = 1;
		} else {
			$this->view->isformenabled = 0;
		}
		
		//CHECK THAT SITEPAGE INVITE PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageinvite' ) && $ismoduleenabled) {
			$this->view->isinviteenabled = 1;
		} else {
			$this->view->isinviteenabled = 0;
		}
		
		//CHECK THAT SITEPAGE BADGE PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagebadge' ) && $ismoduleenabled) {
			$this->view->isbadgeenabled = 1;
		} else {
			$this->view->isbadgeenabled = 0;
		}
		
		//CHECK THAT SITEPAGE NOTE PLUGIN IS ENABLED OR NOT
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' ) && $ismoduleenabled) {
			$this->view->ismusicenabled = 1;
		} else {
			$this->view->ismusicenabled = 0;
		}
		
		//CHECK FORM VALIDATION
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			//GET FORM VALUES
			$values = $form->getValues ();
			include APPLICATION_PATH . '/application/modules/Sitepage/controllers/license/license2.php';
		}
	}
	
	public function setDefaultMapCenterPoint($oldLocation, $newLocation) {
		if ($oldLocation !== $newLocation) {
			if ($newLocation !== "World" && $newLocation !== "world") {
				$urladdress = str_replace ( " ", "+", $newLocation );
				//Initialize delay in geocode speed
				$delay = 0;
				//Iterate through the rows, geocoding each address
				$geocode_pending = true;
				while ( $geocode_pending ) {
					$request_url = "http://maps.googleapis.com/maps/api/geocode/json?address=$urladdress&sensor=true";
					$ch = curl_init ();
					$timeout = 0;
					curl_setopt ( $ch, CURLOPT_URL, $request_url );
					curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
					ob_start ();
					curl_exec ( $ch );
					curl_close ( $ch );
					$json_resopnse = Zend_Json::decode ( ob_get_contents () );
					ob_end_clean ();
					$status = $json_resopnse ['status'];
					if (strcmp ( $status, "OK" ) == 0) {
						//Successful geocode
						$geocode_pending = false;
						$result = $json_resopnse ['results'];
						
						//Format: Longitude, Latitude, Altitude
						$lat = $result [0] ['geometry'] ['location'] ['lat'];
						$lng = $result [0] ['geometry'] ['location'] ['lng'];
					} else if (strcmp ( $status, "620" ) == 0) {
						//sent geocodes too fast
						$delay += 100000;
					} else {
						//failure to geocode
						$geocode_pending = false;
						echo "Address " . $locationLocal . " failed to geocoded. ";
						echo "Received status " . $status . "\n";
					}
					usleep ( $delay );
				}
			} else {
				$lat = 0;
				$lng = 0;
			}
			
			Engine_Api::_ ()->getApi ( 'settings', 'core' )->setSetting ( 'sitepage.map.latitude', $lat );
			Engine_Api::_ ()->getApi ( 'settings', 'core' )->setSetting ( 'sitepage.map.longitude', $lng );
		}
	}
	
	//ACTINO FOR SEARCH
	public function formSearchAction() {
		
		// GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_form_search' );
		
		$table = Engine_Api::_ ()->getDbTable ( 'searchform', 'sitepage' );
		
		//CHECK POST
		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			$values = $_POST;
			$row = $table->getFieldsOptions ( 'profile_type' );
			$defaultAddition = 0;
			$rowCategory = $table->getFieldsOptions ( 'category_id' );
			$defaultCategory = 0;
			try {
				foreach ( $values ['order'] as $key => $value ) {
					$table->update ( array ('order' => $defaultAddition + $defaultCategory + $key + 1 ), array ('searchform_id =?' => ( int ) $value ) );
					if (! empty ( $row ) && $value == $row->searchform_id)
						$defaultAddition = 10000000;
					
					if (! empty ( $rowCategory ) && $value == $rowCategory->searchform_id)
						$defaultCategory = 1;
				}
				$db->commit ();
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		}
		$this->view->enableBadgePlugin = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagebadge' );
		$this->view->enableReviewPlugin = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' );
		$this->view->enableGeoLocationPlugin = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagegeolocation' );
		$this->view->searchForm = $table->fetchAll ( $table->select ()->order ( 'order' ) );
	}
	
	//ACTINO FOR ACTIVITY FEED
	public function activityFeedAction() {
		// GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_activity_feed' );
		
		//FILTER FORM
		$this->view->form = $form = new Sitepage_Form_Admin_Settings_ActivityFeed ();
		//CHECK POST
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = $form->getValues ();
			$api = Engine_Api::_ ()->getApi ( "settings", "core" );
			foreach ( $values as $key => $value ) {
				$api->setSetting ( $key, $value );
			}
			$enable = $form->sitepage_feed_type->getValue ();
			$db = Zend_Db_Table_Abstract::getDefaultAdapter ();
			$activityfeed_array = array ("sitepagealbum_admin_photo_new", "sitepagedocument_admin_new", "sitepageevent_admin_new", "sitepagemusic_admin_new", "sitepagenote_admin_new", "sitepageoffer_admin_new", "sitepagepoll_admin_new", "sitepagevideo_admin_new", "sitepage_admin_topic_create" );
			foreach ( $activityfeed_array as $value ) {
				$activit_type_sql = "UPDATE `engine4_activity_actiontypes` SET `enabled` = $enable WHERE `engine4_activity_actiontypes`.`type` = '$value' LIMIT 1";
				$db->query ( $activit_type_sql );
			}
		}
	}
	
	public function overwriteAction() {
		$type = $this->_getParam ( 'type' );
		$this->view->error = null;
		$this->view->status = false;
		if (empty ( $type ))
			return;
		
		$moduleActivity = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->getModule ( 'activity' );
		$activityVersion = $moduleActivity->version;
		$dirName = 'activity-' . $activityVersion;
		$sourcePath = $dirName;
		$destinationPath = null;
		$api = Engine_Api::_ ()->getApi ( "settings", "core" );
		$feedType = $this->_getParam ( 'feedType', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.feed.type', 0 ) );
		$likedPage = $this->_getParam ( 'likedPage', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.feed.likepage', 0 ) );
		$everyOne = $this->_getParam ( 'everyOne', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.feed.everyone', 0 ) );
		$api->setSetting ( 'sitepage_feed_type', $feedType );
		$api->setSetting ( 'sitepage_feed_likepage', $likedPage );
		$api->setSetting ( 'sitepage_feed_everyone', $everyOne );
		switch ($type) {
			case 'activity_feed_likepage' :
			case 'activity_feed_everyone' :
				$destinationPath = APPLICATION_PATH . '/application/modules/Activity/Model/DbTable/Actions.php';
				$sourcePath .= '/Model/DbTable/Actions.php';
				
				$api->setSetting ( 'sitepagefeed_likepage_dummy', 'b' );
				$api->setSetting ( 'sitepagefeed_everyone_dummy', 'b' );
				break;
			case 'activity_feed_type' :
				$destinationPath = APPLICATION_PATH . '/application/modules/Activity/controllers/IndexController.php';
				$sourcePath .= '/controllers/IndexController.php';
				$api->setSetting ( 'sitepagefeed_type_dummy', 'b' );
				break;
		}
		
		if (is_file ( $destinationPath )) {
			@chmod ( $destinationPath, 0777 );
		} else {
			$this->view->error = 'Target File does not exist.';
		}
		
		if (! is_writeable ( $destinationPath )) {
			$this->view->error = 'Target file could not be overwritten. You do not have write permission chmod -R 777 recursively to the directory "/application/modules/Activity/". Please give the recursively write permission to this directory and try again.';
		}
		
		$serverPath = 'http://www.socialengineaddons.com/SocialEngine/SocialengineModules/index.php?path=';
		$sourcePath = $serverPath . @urlencode ( $sourcePath );
		$ch = curl_init ();
		$timeout = 0;
		@curl_setopt ( $ch, CURLOPT_URL, $sourcePath );
		@curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
		@ob_start ();
		@curl_exec ( $ch );
		@curl_close ( $ch );
		if (empty ( $exe_status )) {
			$get_value = @ob_get_contents ();
		}
		if (empty ( $get_value )) {
			$get_value = @file_get_contents ( $oposit_url );
		}
		@ob_end_clean ();
		
		if (! empty ( $get_value )) {
			if (! @file_put_contents ( $destinationPath, $get_value )) {
				$this->view->status = false;
				$this->view->error = 'Target file could not be overwritten. You do not have write permission chmod -R 777 recursively to the directory "/application/modules/Activity/". Please give the recursively write permission to this directory and try again.';
				return;
			}
		} else {
			$this->view->error = 'It seems that you do not have any internet connection that\'s why you are not able to overwrite this file.';
		}
		
		@chmod ( $activityTextPath_Original, 0755 );
		if (empty ( $this->view->error ))
			$this->view->status = true;
	}
	
	public function overwriteActivityWidgetsAction() {
		$db = Zend_Db_Table_Abstract::getDefaultAdapter ();
		if ($this->getRequest ()->isPost ()) {
			$db->query ( "UPDATE `engine4_sitepage_content` SET `name` = 'socialengineaddon.feed' WHERE `name` = 'activity.feed';" );
			$db->query ( "UPDATE `engine4_sitepage_admincontent` SET `name` = 'socialengineaddon.feed' WHERE `name` = 'activity.feed';" );
			$db->query ( "UPDATE `engine4_core_content` SET `engine4_core_content`.`name` = 'socialengineaddon.feed' WHERE `engine4_core_content`.`name` = 'activity.feed' and
`engine4_core_content`.`page_id`=(SELECT  `engine4_core_pages`.`page_id`
FROM `engine4_core_pages`
WHERE  (`engine4_core_pages`.`name` = 'sitepage_index_view' and `engine4_core_pages`.`page_id`= `engine4_core_content`.`page_id`));" );
			$api = Engine_Api::_ ()->getApi ( "settings", "core" );
			$feedType = $this->_getParam ( 'feedType', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.feed.type', 0 ) );
			$likedPage = $this->_getParam ( 'likedPage', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.feed.likepage', 0 ) );
			$everyOne = $this->_getParam ( 'everyOne', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.feed.everyone', 0 ) );
			$api->setSetting ( 'sitepage_feed_type', $feedType );
			$api->setSetting ( 'sitepage_feed_likepage', $likedPage );
			$api->setSetting ( 'sitepage_feed_everyone', $everyOne );
			$api->setSetting ( 'sitepagefeed_type_dummy', 'a' );
			$api->setSetting ( 'sitepagefeed_everyone_dummy', 'a' );
			$this->_forward ( 'success', 'utility', 'core', array ('parentRefresh' => 3, 'parentRedirectTime' => 1, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'The core "Activity Feed" widget has been successfully replaced with "SocialEngineAddOns Activity Feed" widget for existing Pages.' ) ) ) );
		}
	}

}
?>